package flc.ast.fragment;

import android.graphics.Outline;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.RecDetailActivity;
import flc.ast.adapter.IndicatorAdapter;
import flc.ast.adapter.MyBannerAdapter;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.adapter.UpdateAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private RecommendAdapter recommendAdapter;
    private UpdateAdapter updateAdapter1, updateAdapter2;
    private IndicatorAdapter indicatorAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        mDataBinding.rvRecommendList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.addChildClickViewIds(R.id.ivRecommendItemCollect);
        recommendAdapter.setOnItemClickListener(this);
        recommendAdapter.setOnItemChildClickListener(this);
        mDataBinding.rvUpdateList1.setLayoutManager(new GridLayoutManager(mContext, 2));
        updateAdapter1 = new UpdateAdapter();
        mDataBinding.rvUpdateList1.setAdapter(updateAdapter1);
        updateAdapter1.addChildClickViewIds(R.id.ivUpdateItemCollect);
        updateAdapter1.setOnItemClickListener(this);
        updateAdapter1.setOnItemChildClickListener(this);
        mDataBinding.rvUpdateList2.setLayoutManager(new GridLayoutManager(mContext, 2));
        updateAdapter2 = new UpdateAdapter();
        mDataBinding.rvUpdateList2.setAdapter(updateAdapter2);
        updateAdapter2.addChildClickViewIds(R.id.ivUpdateItemCollect);
        updateAdapter2.setOnItemClickListener(this);
        updateAdapter2.setOnItemChildClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        RecDetailActivity.detail = bean;
        startActivity(RecDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (adapter == recommendAdapter) {
            switch (view.getId()) {
                case R.id.ivRecommendItemCollect:
                    List<StkResBean> listSp = AppSpUtils.getCollectList();
                    View itemView = mDataBinding.rvRecommendList.getChildAt(position);
                    TextView textView = itemView.findViewById(R.id.tvRecommendItemCollect);
                    ImageView imageView = itemView.findViewById(R.id.ivRecommendItemCollect);
                    int num = Integer.parseInt(textView.getText().toString());
                    if (recommendAdapter.getItem(position).isSelected()) {
                        listSp.remove(recommendAdapter.getItem(position));
                        AppSpUtils.saveCollectList(listSp);
                        recommendAdapter.getItem(position).setSelected(false);
                        num--;
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        recommendAdapter.getItem(position).setSelected(true);
                        listSave.add(recommendAdapter.getItem(position));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        num++;
                    }
                    textView.setText(num + "");
                    if (recommendAdapter.getItem(position).isSelected()) {
                        imageView.setImageResource(R.drawable.sc2);
                    } else {
                        imageView.setImageResource(R.drawable.sc1);
                    }
                    break;
            }
        } else if (adapter == updateAdapter1) {
            switch (view.getId()) {
                case R.id.ivUpdateItemCollect:
                    List<StkResBean> listSp = AppSpUtils.getCollectList();
                    View itemView = mDataBinding.rvUpdateList1.getChildAt(position);
                    TextView textView = itemView.findViewById(R.id.tvUpdateItemCollect);
                    ImageView imageView = itemView.findViewById(R.id.ivUpdateItemCollect);
                    int num = Integer.parseInt(textView.getText().toString());
                    if (updateAdapter1.getItem(position).isSelected()) {
                        listSp.remove(updateAdapter1.getItem(position));
                        AppSpUtils.saveCollectList(listSp);
                        updateAdapter1.getItem(position).setSelected(false);
                        num--;
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        updateAdapter1.getItem(position).setSelected(true);
                        listSave.add(updateAdapter1.getItem(position));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        num++;
                    }
                    textView.setText(num + "");
                    if (updateAdapter1.getItem(position).isSelected()) {
                        imageView.setImageResource(R.drawable.sc2);
                    } else {
                        imageView.setImageResource(R.drawable.sc1);
                    }
                    break;
            }
        } else if (adapter == updateAdapter2) {
            switch (view.getId()) {
                case R.id.ivUpdateItemCollect:
                    List<StkResBean> listSp = AppSpUtils.getCollectList();
                    View itemView = mDataBinding.rvUpdateList2.getChildAt(position);
                    TextView textView = itemView.findViewById(R.id.tvUpdateItemCollect);
                    ImageView imageView = itemView.findViewById(R.id.ivUpdateItemCollect);
                    int num = Integer.parseInt(textView.getText().toString());
                    if (updateAdapter2.getItem(position).isSelected()) {
                        listSp.remove(updateAdapter2.getItem(position));
                        AppSpUtils.saveCollectList(listSp);
                        updateAdapter2.getItem(position).setSelected(false);
                        num--;
                    } else {
                        List<StkResBean> listSave = new ArrayList<>();
                        updateAdapter2.getItem(position).setSelected(true);
                        listSave.add(updateAdapter2.getItem(position));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        num++;
                    }
                    textView.setText(num + "");
                    if (updateAdapter2.getItem(position).isSelected()) {
                        imageView.setImageResource(R.drawable.sc2);
                    } else {
                        imageView.setImageResource(R.drawable.sc1);
                    }
                    break;
            }
        }
    }

    @Override
    protected void initData() {
        List<Integer> listBanner = new ArrayList<>();
        listBanner.add(R.drawable.banner1);
        listBanner.add(R.drawable.banner2);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(listBanner, mContext);
        mDataBinding.homeBanner.setClipToOutline(true);
        mDataBinding.homeBanner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.homeBanner.isAutoLoop(true);//false不自动轮播
        mDataBinding.homeBanner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorAdapter.setSelPosition(position);
                indicatorAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mDataBinding.rvIndicatorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        indicatorAdapter = new IndicatorAdapter();
        indicatorAdapter.setSelPosition(0);
        indicatorAdapter.setList(listBanner);
        mDataBinding.rvIndicatorList.setAdapter(indicatorAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        getRecommendData();
        getUpdateData();
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 3), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<StkResBean> listSp = AppSpUtils.getCollectList();
                        for (StkResBean bean : result) {
                            if (listSp != null && listSp.size() != 0) {
                                for (StkResBean stkResBean : listSp) {
                                    if (bean.equals(stkResBean)) {
                                        bean.setSelected(true);
                                        break;
                                    }
                                }
                            } else {
                                break;
                            }
                        }
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getUpdateData() {
        StkResApi.getTagResourceList(this, AppConfig.UPDATE_DATA, StkResApi.createParamMap(0, 4), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        List<StkResBean> listSp = AppSpUtils.getCollectList();
                        for (StkResBean bean : result) {
                            if (listSp != null && listSp.size() != 0) {
                                for (StkResBean stkResBean : listSp) {
                                    if (bean.equals(stkResBean)) {
                                        bean.setSelected(true);
                                        break;
                                    }
                                }
                            } else {
                                break;
                            }
                        }
                        updateAdapter1.setList(result.subList(0, 2));
                        updateAdapter2.setList(result.subList(2, 4));
                    }
                }
            }
        });
    }

}